
module Penn
 class GenericObject < Mu::MuObject
   attr_accessor :name,:location,:dbref,:zone,:owner,:connection,:type,:locks,:flags,:home
   def initialize(name="",location="",dbref="",zone="",owner="",flags = [], locks = {},type=:OBJECT)
     super(name)
     @attributes[:at]={}
     @attributes[:and]={}
     @location,@dbref,@zone,@owner=location,dbref,zone,owner
     @flags = flags
     @locks = locks
#      @connection = connection
     @type = type
   end
 end
 class RoomObject < GenericObject

 end
 class PlayerObject < GenericObject
   def initialize(*args)
     super(*args)
     type = :PLAYER
     @type = type
   end
 end
 class MuObject < GenericObject

 end
 class Attribute < Mu::MuAttribute
   attr_accessor :type,:flags,:locks
   def initialize(name,value,type)
     super name,value
     @type = type
     @flags = []
     @locks = {}
   end
 end
 # must provide the realiseObject
 # return the header,footer regexp for examine
 # return the attribute,value regexp
 def Penn::sync_from_yaml obj,type,att,socket
   db=obj.dbref
   str = "&#{att} #{db}=#{obj.attributes[:at][att]}"
   socket.puts str
 end
 def Penn::realiseObject(obj=nil,dbref=nil,client=nil)
   # start and rend acky cuase its not in the return! but before and after !!
   # getObjectAttribute will be better becuase we can put them into the return
   if dbref == nil
     dbref = obj.dbref
   end
   if obj  == nil and dbref != nil
    obj = Penn::GenericObject.new
    obj.connection = client
    obj.dbref = dbref
    Penn::set_object(obj,client)
   end
# p dbref
#  
   command = "@decompile #{dbref}"
   set_object = /^\@set (.*)=(.*)/
   at_atty = /^\@([A-Z]+) (.*)\=(.*)/
   set_atty = /^\@set (.*)\/([A-Z]+)\=(.*)/
   and_atty = /^\&([A-Z]+) (.*)\=(.*)/
   lock = /^\@lock\/(.*) (.*)=(.*)/
   lock_set = /^\@lset (.*)\/(.*)=(.*)/
   last_atty = nil
   last_lock = nil
# 
   client.recieve_data(command,1).each_line do |l|
# p l
     l = l.gsub("\r",'')
     if l =~ at_atty
# p l +"======================"
       name,object,value = $1,$2,$3
       attribute = Penn::Attribute::new(name,value,:at)
       obj.attributes[:at][name.to_sym]={}
       obj.attributes[:at][name.to_sym][:data]=attribute
       last_atty = attribute
     end
     if l=~set_object
       obj.flags << $2
     end
     if l =~ lock
# p $1.to_sym
# p obj.instance_variable_get("@locks")
# # p obj
# exit
       obj.locks[$1.to_sym]={:data=>$3,:flags=>[]}
#        obj.locks[$1.to_sym][:data]=$3
     end
     if l =~ lock_set
       obj.locks[$2.to_sym][:flags] << $3
     end
     if l=~set_atty
       # sanity! since @set does not show if an atty is @ or &
       # we go in order. but incase order is messed up, make sure last atty an atty have same name
       if last_atty != nil
         last_atty.flags = $3.split(" ") if $2 == last_atty.name
       end
     end
     if l =~ and_atty
       name,object,value = $1,$2,$3
       attribute = Penn::Attribute::new(name,value,:and)
       obj.attributes[:and][name.to_sym]={}
       obj.attributes[:and][name.to_sym][:data]=attribute
       last_atty = attribute
     end
   end
   return obj
 end
 def Penn::get(attribute,dbref,client)
 # this is nice, the header and footer is in the return string
     command = "th #{client::get_header}%r[get(#{dbref}/#{attribute})]%r#{client::get_footer}"
     value = client.recieve_data(command,0)
     return value
 end
 def Penn::flags(dbref,attribute = nil,client=nil)
 # this is nice, the header and footer is in the return string
     if attribute == nil
       command = "th #{client::get_header}%r[flags(#{dbref})]%r#{client::get_footer}"
     else
       command = "th #{client::get_header}%r[flags(#{dbref}/#{attribute})]%r#{client::get_footer}"
     end
     value = client.recieve_data(command,1)
     return value
 end
 def Penn::set_object(obj,client)
   client.send_wait << ""
# p obj.dbref
   name = client.recieve_data("th #{client::get_header}%r[name(#{obj.dbref})]%r#{client::get_footer}%r",0) 
   if obj.dbref == nil
     client.send_wait << ""
     dbref = obj.recieve_data("th #{client::get_header}%r%#%r#{client::get_footer}%r",0)
   else
     dbref = obj.dbref
   end
   client.send_wait << ""
   home = obj.recieve_data("th #{client::get_header}%r[home(#{dbref})]%r#{client::get_footer}%r",0)
# p dbref 
# exit
   client.send_wait << ""
   location = client.recieve_data("th #{client::get_header}%r[loc(#{obj.dbref})]%r#{client::get_footer}%r",0)
   client.send_wait << ""
   owner = client.recieve_data("th #{client::get_header}%r[owner(#{obj.dbref})]%r#{client::get_footer}%r",0)
   client.send_wait << ""
   type = client.recieve_data("th #{client::get_header}%r[type(#{obj.dbref})]%r#{client::get_footer}%r",0)
   obj.home = home.gsub("\r",'').gsub("\n",'')
   obj.name = name.gsub("\r",'').gsub("\n",'')
   obj.dbref = dbref.gsub("\r",'').gsub("\n",'')
   obj.location = location.gsub("\r",'').gsub("\n",'')
   obj.owner = owner.gsub("\r",'').gsub("\n",'')
   obj.type = type.gsub("\r",'').gsub("\n",'')
   obj.flags << type.gsub("\r",'').gsub("\n",'')
# p obj
# 
 end
end